﻿using GodSystemBLL;
using GodSystemCommon;
using GodSystemModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormProductCate : Form
    {
        public static CategoriesModel categoriesModel;
        public FormProductCate()
        {
            InitializeComponent();
            InitData();
        }

        private void InitData()
        {
            int sizeWidth = 80;
            int sizeHeight = 24;
            int locationX = 280;
            int locationY = 28;
            int sep = 40;

            #region 权限控制可见性
            if (!Utility.CheckUserPower("CateAdd"))
            {
                button_Add.Visible = false;
            }
            else
            {
                button_Add.Visible = true;
                button_Add.Location = new Point(locationX, locationY);
                button_Add.Size = new Size(sizeWidth, sizeHeight);
                locationX += sizeWidth + sep;
            }

            if (!Utility.CheckUserPower("CateUpdate"))
            {
                button_Update.Visible = false;
            }
            else
            {
                button_Update.Visible = true;
                button_Update.Location = new Point(locationX, locationY);
                button_Update.Size = new Size(sizeWidth, sizeHeight);
            }

            #endregion

            #region 展示分类列表

            CateBLL cateBLL = new CateBLL();
            dataGridView_CateList.DataSource = cateBLL.GetCategories();

            #endregion
        }

        private void button_Add_Click(object sender, EventArgs e)
        {
            FormCateAdd formCateAdd = new FormCateAdd();
            formCateAdd.Activate();
            formCateAdd.Show();
        }

        private void button_Update_Click(object sender, EventArgs e)
        {
            // 获取表格当前选择的行,即使点击是单元格，也可以获取整个行
            int index = dataGridView_CateList.CurrentRow.Index;
            if (index < 0)
            {
                MessageBox.Show("请选择要修改的分类", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string idStr = dataGridView_CateList.Rows[index].Cells[0].Value.ToString();
            string cateName = dataGridView_CateList.Rows[index].Cells[1].Value.ToString();
            int id = Convert.ToInt32(idStr);
            categoriesModel = new CategoriesModel();
            categoriesModel.Id = id;
            categoriesModel.CateName = cateName;
            // 实例化在后
            // FormUserModify formUserModify = new FormUserModify();
            FormCateUpdate formCateUpdate = FormCateUpdate.CreateInstance();

            formCateUpdate.Activate();
            formCateUpdate.Show();
        }
    }
}
